package com.android.baseconfig.base;

import android.content.Context;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import java.util.Set;

/**
 * @author lei
 * @date 2017/5/10
 */

public class BaseSharedPreferences {

    private static final String COMMON_PRE = "common_pre";
    // 用户标识缓存
    public static final String USER_ID = "accountID";
    /**
     * 缓存本地路径存储路径的标志
     */
    public static final String CAMERA_STORAGE_PATH = "storagePath-";
    // 判断是否首次登陆
    public static final String FIRST_COMING = "firstComing";
    private static Context context;

    private BaseSharedPreferences() {

    }

    /**
     * 清除Cookie
     *
     * @param context
     */
    public static void removeCookie(Context context) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        CookieSyncManager.getInstance().sync();
    }

    public static boolean contains(Context context, String key) {
        return context.getSharedPreferences(COMMON_PRE, 0).contains(key);
    }

    public static boolean getBoolean(Context context, String key, boolean defValue) {
        return context.getSharedPreferences(COMMON_PRE, 0).getBoolean(key, defValue);
    }

    public static boolean getBoolean(Context context, String shareName, String key) {
        return context.getSharedPreferences(shareName, 0).getBoolean(key, false);
    }

    public static int getInt(Context context, String key) {
        return context.getSharedPreferences(COMMON_PRE, 0).getInt(key
                , 0);
    }

    public static int getInt(Context context, String key, int defValue) {
        return context.getSharedPreferences(COMMON_PRE, 0).getInt(key, defValue);
    }

    public static long getLong(Context context, String key) {
        return context.getSharedPreferences(COMMON_PRE, 0).getLong(key, 0L);
    }

    public static String getString(Context context, String key) {
        return context.getSharedPreferences(COMMON_PRE, 0).getString(key, "");
    }

    public static String getString(Context context, String key, String defValue) {
        return context.getSharedPreferences(COMMON_PRE, 0).getString(key, defValue);
    }

    public static String getStringByName(Context context, String shareName, String key) {
        return context.getSharedPreferences(shareName, 0).getString(key, "");
    }

    public static void remove(Context context, String key) {
        context.getSharedPreferences(COMMON_PRE, 0).edit().remove(key).apply();
    }

    public static void setBoolean(Context context, String shareName, String key, boolean value) {
        context.getSharedPreferences(shareName, 0).edit().putBoolean(key, value).apply();
    }

    public static void setBoolean(Context context, String key, boolean value) {
        context.getSharedPreferences(COMMON_PRE, 0).edit().putBoolean(key, value).apply();
    }

    public static void setInt(Context context, String key, int value) {
        context.getSharedPreferences(COMMON_PRE, 0).edit().putInt(key
                , value).apply();
    }

    public static void setLong(Context context, String key, Long value) {
        if (value == null) {
            value = 0L;
        }
        context.getSharedPreferences(COMMON_PRE, 0).edit().putLong(key, value.longValue())
                .apply();
    }

    public static void setString(Context context, String key, String value) {
        context.getSharedPreferences(COMMON_PRE, 0).edit().putString(key, value).apply();
    }

    public static void setString(Context context, String shareName, String key, String value) {
        String str =
                context.getSharedPreferences(COMMON_PRE, 0).getString(USER_ID, "");
        context.getSharedPreferences(str + "_" + shareName, 0).edit().putString(key, value)
                .apply();
    }

    public static void setStringByName(Context context, String shareName, String key,
                                       String value) {
        context.getSharedPreferences(shareName, 0).edit().putString(key, value).apply();
    }

    public static void setStringSet(Context context, String shareName, String key,
                                    Set<String> value) {
        String str =
                context.getSharedPreferences(COMMON_PRE, 0).getString(USER_ID, "");
        context.getSharedPreferences(str + "_" + shareName, 0).edit().putStringSet(key, value)
                .apply();
    }
}
